package org.dnd.rcp.editor;

import org.dnd.rcp.editor.actions.CopyTextAction;
import org.dnd.rcp.editor.actions.CutTextAction;
import org.dnd.rcp.editor.actions.PasteTextAction;
import org.dnd.rcp.editor.text.Editor;
import org.dnd.rcp.editor.text.EditorInput;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

/**
 * Kelas ini merupakan actions untuk menu yang implementasinya ke window
 * 
 * @author dendy
 * 
 */
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	private IWorkbenchAction exitAction;
	private Action newAction;
	private static Action copyAction;
	private static Action pasteAction;
	private static Action cutAction;
	private static IWorkbenchAction saveAction;
	private static IWorkbenchAction saveAllAction;
	private static IWorkbenchAction closeAction;
	private static IWorkbenchAction closeAllAction;

	protected void makeActions(final IWorkbenchWindow window) {

		newAction = new Action() {
			@Override
			public void run() {
				try {
					window.getWorkbench().getActiveWorkbenchWindow()
							.getActivePage()
							.openEditor(new EditorInput(null), Editor.ID);
				} catch (PartInitException e) {
				}
			}
		};
		newAction.setText("New File");
		exitAction = ActionFactory.QUIT.create(window);
		register(exitAction);

		copyAction = new CopyTextAction();
		copyAction.setText(" &Copy\t\tCtrl+C ");
		copyAction.setAccelerator(SWT.CTRL + 'C');
		register(copyAction);

		cutAction = new CutTextAction();
		cutAction.setText(" &Cut\t\tCtrl+X ");
		cutAction.setAccelerator(SWT.CTRL + 'X');
		register(cutAction);

		pasteAction = new PasteTextAction();
		pasteAction.setText(" &Paste\t\tCtrl+V ");
		pasteAction.setAccelerator(SWT.CTRL + 'V');
		register(pasteAction);

		saveAction = ActionFactory.SAVE.create(window);
		register(saveAction);

		saveAllAction = ActionFactory.SAVE_ALL.create(window);
		register(saveAllAction);

		closeAction = ActionFactory.CLOSE.create(window);
		register(closeAction);

		closeAllAction = ActionFactory.CLOSE_ALL.create(window);
		register(closeAllAction);

	}

	protected void fillMenuBar(IMenuManager menuBar) {
		// File menu
		MenuManager fileMenu = new MenuManager("&File",
				IWorkbenchActionConstants.M_FILE);
		fileMenu.add(newAction);
		fileMenu.add(saveAction);
		fileMenu.add(saveAllAction);
		fileMenu.add(new Separator("save"));
		fileMenu.add(new Separator("closers"));
		fileMenu.add(closeAction);
		fileMenu.add(closeAllAction);
		fileMenu.add(new Separator("exiters"));
		fileMenu.add(exitAction);

		// Edit menu
		MenuManager editMenu = new MenuManager("&Edit",
				IWorkbenchActionConstants.M_EDIT);
		editMenu.add(copyAction);
		editMenu.add(cutAction);
		editMenu.add(pasteAction);

		menuBar.add(fileMenu);
		menuBar.add(editMenu);
	}

}
